import AntModal from '@/components/pt/dialog/AntModal'
import { getBizProduct, addBizProduct, updateBizProduct, checkDataUnique } from '@/api/biz/bizProduct'
import pinyin from 'js-pinyin'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
export default {
  name: 'CreateForm',
  props: {
    categoryIdOptions: {
      type: Array,
      required: true
    },
    warehouseList: {
      type: Array,
      required: true
    },
    inventoryOptions: {
      type: Array,
      required: true
    }
    

  },
  components: {
    AntModal,
    AdvanceTable

  },
  data () {
    return {
      currentTab:'first',
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '添加产品',
      // 表单参数
      form: {},
      rules: {
        categoryId: [{ required: true, message: '产品大类不能为空', trigger: 'blur' }],

        mnemonicCode: [{ required: true, message: '助记码不能为空', trigger: 'blur' }]


      },
      unitColumns : [
        {
          title: '单位类型',
          dataIndex: 'unitType',
          width: '10%'
        },
        {
          title: '单位名称',
          dataIndex: 'unitName',
          width: '10%',
          scopedSlots: {
            customRender: 'unitNameSlot'
          }
        },
        {
          title: '换算关系',
          dataIndex: 'convertRatio',
          width: '10%',
          scopedSlots: {
            customRender: 'convertRatioSlot'
          }
        },

        {
          title: '销售价',
          dataIndex: 'salePrice',
          width: '10%',
          scopedSlots: {
            customRender: 'salePriceSlot'
          }
        },
        {
          title: '采购价',
          dataIndex: 'purchasesPrice',
          width: '10%',
          scopedSlots: {
            customRender: 'purchasesPriceSlot'
          }
        },
        {
          title: '销售提成',
          dataIndex: 'saleDeduct',
          width: '10%',
          scopedSlots: {
            customRender: 'saleDeductSlot'
          }
        },
        {
          title: '安装提成',
          dataIndex: 'installDeduct',
          width: '10%',
          scopedSlots: {
            customRender: 'installDeductSlot'
          }
        },
        {
          title: '入库默认',
          dataIndex: 'defaultWarehousingUnit',
          width: '10%',
          scopedSlots: {
            customRender: 'defaultWarehousingUnitSlot'
          }
        },
        {
          title: '出库默认',
          dataIndex: 'defaultOutboundUnit',
          width: '10%',
          scopedSlots: {
            customRender: 'defaultOutboundUnitSlot'
          }
        }
      ],

      unitData:[{
        unitType: '单位',
        unitName: '',
        convertRatio: 1,
        salePrice:'',
        purchasesPrice:'',
        saleDeduct:'',
        installDeduct:'',
        type:1,
        defaultOutboundUnit:1,
        defaultWarehousingUnit:1
      },
        {
          unitType: '辅助单位1',
          unitName: '',
          convertRatio: 1,
          salePrice:'',
          purchasesPrice:'',
          saleDeduct:'',
          installDeduct:'',
          type:2,
          defaultOutboundUnit:0,
          defaultWarehousingUnit:0
        },
        {
          unitType: '辅助单位2',
          unitName: '',
          convertRatio: 1,
          salePrice:'',
          purchasesPrice:'',
          saleDeduct:'',
          installDeduct:'',
          type:3,
          defaultOutboundUnit:0,
          defaultWarehousingUnit:0
        }
        ]
    }
  },
  filters: {},
  created () {
  },
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    onClose () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        categoryId: undefined,

        productCode: undefined,

        mnemonicCode: undefined,

        warehouseId: undefined,

        brand: undefined,

        color: undefined,

        inventoryFlag: '1',

        specLength: undefined,

        specWidth: undefined,

        spec:undefined,

        specHeight: undefined,

        model: undefined,

        warnStock: undefined,

        tenantId: undefined

      }
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.$emit('select-tree')
      this.$emit('select')
      this.open = true
      this.formTitle = '添加产品'
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const bizProductId = row.id
      this.$emit('select-tree');
      this.$emit('select');
      getBizProduct(bizProductId).then(response => {
        this.form = response.data
        this.formTitle = '修改产品'
        this.spinning = !this.spinning
        this.unitData[0].unitName=response.data.unit;
        this.unitData[1].unitName=response.data.assistUnitA;
        this.unitData[2].unitName=response.data.assistUnitB;
        this.unitData[1].convertRatio=response.data.convertRatioA;
        this.unitData[2].convertRatio=response.data.convertRatioB;
        this.unitData[0].salePrice=response.data.salePrice;
        this.unitData[1].salePrice=response.data.salePriceA;
        this.unitData[2].salePrice=response.data.salePriceB;
        this.unitData[0].purchasesPrice=response.data.purchasesPrice;
        this.unitData[1].purchasesPrice=response.data.purchasesPriceA;
        this.unitData[2].purchasesPrice=response.data.purchasesPriceB;
        this.unitData[0].saleDeduct=response.data.saleDeduct;
        this.unitData[1].saleDeduct=response.data.saleDeductA;
        this.unitData[2].saleDeduct=response.data.saleDeductB;
        this.unitData[0].installDeduct=response.data.installDeduct;
        this.unitData[1].installDeduct=response.data.installDeductA;
        this.unitData[2].installDeduct=response.data.installDeductB;

        this.unitData[0].defaultOutboundUnit=false;
        this.unitData[1].defaultOutboundUnit=false;
        this.unitData[2].defaultOutboundUnit=false;
        // 出库默认
        this.unitData[response.data.defaultOutboundUnit - 1].defaultOutboundUnit= true;

        this.unitData[0].defaultWarehousingUnit=false;
        this.unitData[1].defaultWarehousingUnit=false;
        this.unitData[2].defaultWarehousingUnit=false;
        // 入库默认
        this.unitData[response.data.defaultWarehousingUnit - 1].defaultWarehousingUnit= true;

      })
    },
    /** 提交按钮 */
    async submitForm(closeDialog) {
      const flag = await this.checkDataUnique()
      if(!flag){
        this.$message.error('产品信息重复')
        return;
      }
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.id !== undefined) {
            saveForm.unit=this.unitData[0].unitName;
            saveForm.convertRatio=this.unitData[0].convertRatio;
            saveForm.salePrice=this.unitData[0].salePrice;
            saveForm.purchasesPrice=this.unitData[0].purchasesPrice;
            saveForm.saleDeduct=this.unitData[0].saleDeduct;
            saveForm.installDeduct=this.unitData[0].installDeduct;
            saveForm.assistUnitA=this.unitData[1].unitName;
            saveForm.convertRatioA=this.unitData[1].convertRatio;
            saveForm.salePriceA=this.unitData[1].salePrice;
            saveForm.purchasesPriceA=this.unitData[1].purchasesPrice;
            saveForm.saleDeductA=this.unitData[1].saleDeduct;
            saveForm.installDeductA=this.unitData[1].installDeduct;

            saveForm.assistUnitB=this.unitData[2].unitName;
            saveForm.convertRatioB=this.unitData[2].convertRatio;
            saveForm.salePriceB=this.unitData[2].salePrice;
            saveForm.purchasesPriceB=this.unitData[2].purchasesPrice;
            saveForm.saleDeductB=this.unitData[2].saleDeduct;
            saveForm.installDeductB=this.unitData[2].installDeduct;
            this.unitData.forEach(unit=>{
              if(unit.defaultOutboundUnit==1) {
                saveForm.defaultOutboundUnit=unit.type;
              }
              if(unit.defaultWarehousingUnit==1) {
                saveForm.defaultWarehousingUnit=unit.type;
              }
            })
            updateBizProduct(saveForm).then(response => {
              this.$message.success('更新成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          } else {
            saveForm.unit=this.unitData[0].unitName;
            saveForm.convertRatio=this.unitData[0].convertRatio;
            saveForm.salePrice=this.unitData[0].salePrice;
            saveForm.purchasesPrice=this.unitData[0].purchasesPrice;
            saveForm.saleDeduct=this.unitData[0].saleDeduct;
            saveForm.installDeduct=this.unitData[0].installDeduct;
            saveForm.assistUnitA=this.unitData[1].unitName;
            saveForm.convertRatioA=this.unitData[1].convertRatio;
            saveForm.salePriceA=this.unitData[1].salePrice;
            saveForm.purchasesPriceA=this.unitData[1].purchasesPrice;
            saveForm.saleDeductA=this.unitData[1].saleDeduct;
            saveForm.installDeductA=this.unitData[1].installDeduct;

            saveForm.assistUnitB=this.unitData[2].unitName;
            saveForm.convertRatioB=this.unitData[2].convertRatio;
            saveForm.salePriceB=this.unitData[2].salePrice;
            saveForm.purchasesPriceB=this.unitData[2].purchasesPrice;
            saveForm.saleDeductB=this.unitData[2].saleDeduct;
            saveForm.installDeductB=this.unitData[2].installDeduct;
            this.unitData.forEach(unit=>{
              if(unit.defaultOutboundUnit==1) {
                saveForm.defaultOutboundUnit=unit.type;
              }
              if(unit.defaultWarehousingUnit==1) {
                saveForm.defaultWarehousingUnit=unit.type;
              }
            })
            addBizProduct(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              this.open = false
              this.$emit('ok',this.form.mnemonicCode)
              this.$emit('close')
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back () {
      const index = '/biz/bizproduct/index'
      this.$router.push(index)
    },
    checkDataUnique(){
      return new Promise((resolve, reject) => {
        checkDataUnique(this.form).then(res=>{
          if(res.data.code === '1'){
            resolve(true)
          }else{
            resolve(false)
          }
        })
      })
    },
    handleTabChange (activeKey) {
      this.currentTab = activeKey;
    },
    genMnemonicCode() {
      let code = "";
      if(this.form.brand) {
        code+=this.form.brand;
      }
      if(this.form.color) {
        code+=this.form.color;
      }
      if(this.form.spec) {
        code+=this.form.spec;
      }
      if(this.form.model) {
        code+=this.form.model;
      }
      if(!code){
        this.$message.error('最少填一项产品信息')
        return;
      }
      this.form.mnemonicCode=pinyin.getCamelChars(code)
    },
    genSpec() {
      this.form.spec="";
      if(this.form.specLength) {
        this.form.spec+=this.form.specLength+"";
      }
      if(this.form.specWidth) {
        if(this.form.spec) {
          this.form.spec+="*";
        }
        this.form.spec+=this.form.specWidth+"";
      }
      if(this.form.specHeight) {
        if(this.form.spec) {
          this.form.spec+="*";
        }
        this.form.spec+=this.form.specHeight+"";
      }
      this.genMnemonicCode();
    },
    clickWarehousingUnit(record) {
      this.unitData.forEach(unit=>{
        if(unit.type!=record.type && record.defaultWarehousingUnit==1) {
          unit.defaultWarehousingUnit=0;
        }
      })
    },
    clickOutboundUnit(record) {
      this.unitData.forEach(unit=>{
        if(unit.type!=record.type && record.defaultOutboundUnit==1) {
          unit.defaultOutboundUnit=0;
        }
      })
    },
    /**
     * 
     * @param {*} value 
     * @param {*} label 
     * @param {*} extra 
     */
    treeChange(value, label, extra){
      label && label[0] && label[0] === '费用' && this.$set(this.form,'inventoryFlag','0') || this.$set(this.form,'inventoryFlag','1')
    },

  }
}
